package com.kamistoat.meimeistore.member.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kamistoat.common.runTimeFunc.RunTimeLog;
import com.kamistoat.meimeistore.member.dao.MemberLoginLogDao;
import com.kamistoat.meimeistore.member.entity.MemberLoginLogEntity;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class DeleteLoginLogScheduled {
    @Autowired
    MemberLoginLogDao memberLoginLogDao;

    @Autowired
    RedissonClient redissonClient;  // 分布式锁可用可不用，因为删除操作可以不关心迸发问题。
    private final String delete_log = "member:delete:log";

//    @Scheduled(cron = "0 30 3 * * ?")  // 每晚3点半
    @Scheduled(cron = "30 0/30 * * * ?")  // 每30分钟的30秒
//    @Scheduled(cron = "30 * * * * ?")  // 每分钟的30秒执行一次
    public void delete30DayLoginLog(){
        Long curDate = new Date().getTime();
        Long tarDate = curDate - 1000 * 60 * 60 * 24;  // 删除1天前的登录记录
        SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        RunTimeLog.printRunTimeLog("删除 " + dateFormat.format(new Date(tarDate)) + " 前的登录记录");
        // 删除可以直接用MySQL自带的比较符删除
        memberLoginLogDao.delete(new QueryWrapper<MemberLoginLogEntity>().lt("create_time", new Date(tarDate)));
    }
}
